package com.example.zhou.connect;


import com.example.zhou.callback.LoadingPageCallback;
import com.example.zhou.url.ZhihuUrl;


import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by 11065 on 2016/10/26.
 */

public class LoadingPageConnect implements LoadingPageInterface {


    @Override
    public void onLoadingPage(final LoadingPageCallback callback) {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(ZhihuUrl.FIRSTPAGEIMAGE)
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                String code = e.getMessage();
                callback.onLoadingPageFail(code);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                callback.onLoadingPageSuccess(response.body().string());
            }

        });
    }


}

interface LoadingPageInterface {
    void onLoadingPage(LoadingPageCallback callback);
}




